package com.hfy.pcberpbackend.controller;

import com.hfy.pcberpbackend.entity.Material;
import com.hfy.pcberpbackend.service.MaterialService;
import com.hfy.pcberpbackend.common.Result;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 物料Controller
 */
@Slf4j
@RestController
@RequestMapping("/api/materials")
@RequiredArgsConstructor
@Validated
public class MaterialController {
    
    private final MaterialService materialService;
    
    /**
     * 模糊搜索物料
     * 支持按物料编码、物料名称进行模糊查询
     */
    @GetMapping
    public Result<List<Material>> searchMaterials(
            @RequestParam String keyword,
            @RequestParam(defaultValue = "20") Integer size) {
        log.info("模糊搜索物料，关键词：{}，数量限制：{}", keyword, size);
        
        try {
            List<Material> materials = materialService.searchMaterials(keyword, size);
            return Result.success("搜索成功", materials);
        } catch (Exception e) {
            log.error("模糊搜索物料失败", e);
            return Result.error("模糊搜索物料失败：" + e.getMessage());
        }
    }
} 